﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace ZGameFramework.Config
{
    public class ConfigHepler: IConfigHelper
    {
        Dictionary<string, ConfigCfgData> m_dictData = new Dictionary<string, ConfigCfgData>();

        /// <summary>
        /// 加载数据
        /// </summary>
        public void Load()
        {
            Assembly assembly = Assembly.GetCallingAssembly();
            foreach (string cfgDataName in CfgVar.m_CfgDatas)
            {
                object obj = assembly.CreateInstance("ZGameFramework.Config."+cfgDataName);
                ConfigCfgData cfgData = obj as ConfigCfgData;
                if (null != cfgData)
                {
                    cfgData.Load();

                    m_dictData.Add( cfgDataName, cfgData);
                }
            }
        }

        /// <summary>
        /// 获取配置文件数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public T GetCfgData<T>() where T : ConfigCfgData
        {
            T result = null;
            ConfigCfgData cfgData = null;
            if (m_dictData.TryGetValue(typeof(T).Name, out cfgData))
            {
                result = cfgData as T;
            }

            return result;
        }
    }
}
